package com.impetus.project.skilltest.server.dao;

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.impetus.project.skilltest.common.SkillTestException;
import com.impetus.project.skilltest.common.dataobject.ProgrammingQuestion;
import com.impetus.project.skilltest.server.util.HibernateUtil;

public class ProgrammingQuestionDAO {
	
	private static Logger logger = Logger.getLogger(ProgrammingQuestionDAO.class.getName());
	private static SessionFactory sessionFactory = HibernateUtil.getSessionFactory();
	
	/*
	private static final String TEST_QUESTIONS_SQL = new StringBuffer().append("select QUESTION_ID, Q_DOMAIN,")
		.append(" COMPLEXITY, Q_TEXT, Q_IMG, METHOD_NAME, RETURN_TYPE, ARG1, ARG2, ARG3, ARG4, TEST_DATA_ARG1, TEST_DATA_ARG2, ")
		.append("TEST_DATA_ARG3, TEST_DATA_ARG4, TEST_DATA_OUTPUT, IS_ENABLED, CREATEDBY, CREATEDON, MODIFIEDBY, ")
		.append("MODIFIEDON from " )
		.append(" ProgrammingQuestion where Q_DOMAIN = ? and COMPLEXITY = ? and IS_ENABLED = Y " )
		.append(" ORDER BY RAND() limit ?").toString();
	*/
	
	@SuppressWarnings("unchecked")
	public static List<ProgrammingQuestion> getProgrammingQuestionList ( String domain, String complexity, int count )
		throws SkillTestException
	{
		List<ProgrammingQuestion> programmingQuestionList = new ArrayList<ProgrammingQuestion>();
		
		try{
			sessionFactory.getCurrentSession().beginTransaction();
			programmingQuestionList = sessionFactory.getCurrentSession()
			.createCriteria(ProgrammingQuestion.class)
			.add(Restrictions.like("Q_DOMAIN", domain))
			.add(Restrictions.like("COMPLEXITY", complexity))
			.add(Restrictions.like("IS_ENABLED", "Y"))
			.add(Restrictions.eq("limit", count))
			.addOrder(Order.asc("RAND()"))
			.list();			
		}
		catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}		
		
		return programmingQuestionList;		
	}
	
	@SuppressWarnings("unchecked")
	public static List<ProgrammingQuestion> getProgrammingQuestionById ( String questionId)
	throws SkillTestException
	{
		List<ProgrammingQuestion> programmingQuestionList = new ArrayList<ProgrammingQuestion>();
		
		try{
			sessionFactory.getCurrentSession().beginTransaction();
			programmingQuestionList = sessionFactory.getCurrentSession()
			.createCriteria(ProgrammingQuestion.class)
			.add(Restrictions.like("QUESTION_ID", questionId))
			.list();			
		}
		catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}		
		
		return programmingQuestionList;
	}
	
	public static void saveUpdateProgrammingQuestion(ProgrammingQuestion programmingQuestion)
	throws SkillTestException
	{
		try
		{
			sessionFactory.getCurrentSession().beginTransaction();
			sessionFactory.getCurrentSession().saveOrUpdate(programmingQuestion);
			sessionFactory.getCurrentSession().getTransaction().commit();
		}
		catch(Exception e)
		{
			sessionFactory.getCurrentSession().getTransaction().rollback();
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
	}	
	
	public static void deleteProgrammingQuestion(ProgrammingQuestion programmingQuestion)
	throws SkillTestException
	{
		try
		{
			sessionFactory.getCurrentSession().beginTransaction();
			sessionFactory.getCurrentSession().delete(programmingQuestion);
			sessionFactory.getCurrentSession().getTransaction().rollback();
		}
		catch(Exception e)
		{
			logger.log(Level.INFO, e.getMessage());
			throw new SkillTestException(e);
		}
	}
}
